<script setup lang="ts">
import {ref, toRefs} from 'vue'
import {useRoute, useRouter} from 'vue-router'
import { useRequest } from "vue-hooks-plus";
import {message} from "ant-design-vue";
import { setToken,setUser } from "@/config";
import { getUserInfo } from "@/apis/student";
import * as jwt from "jwt-decode";

const route = useRoute()
const router = useRouter()
const {params} = toRefs(route)
if(!params.value.account){
  router.push('/')
}
let loading = ref<boolean>(true)
const token:string|any = params.value.account  //地址携带token == params.value.account
const user:{exp:number,userName:string,timeStamp:string} = jwt.jwtDecode(token)
// 使用token携带的关键信息查询用户的完整信息
useRequest(() => getUserInfo({ account: user.userName }), {
  debugKey: 'session',
  onSuccess: (res) => {
    // 设置token信息
    setToken(token)
    setUser(JSON.stringify(res.data))
    loading.value=false
    router.push('/student')
  },
  onError: (err) => {
    message.warning('出错了...')
    console.log(err)
  }
})
</script>

<template>
  <div class="login-loading">
    <a-spin tip="正在读取数据..." size="large" :spinning="loading"></a-spin>
  </div>
</template>

<style scoped lang="scss">
.login-loading{
  position: absolute;
  width: 100%;
  height: 100%;
  background-image: url("./image/页面加载中.png");
  background-repeat: no-repeat;
  background-size:contain;
  background-attachment:fixed;
  background-position:center center;
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>
